package cn.lichenes.controller;

import cn.lichenes.entity.User;
import cn.lichenes.entity.UserStatus;
import cn.lichenes.log.SystemLog;
import cn.lichenes.service.UserService;
import cn.lichenes.utils.MailBox;
import cn.lichenes.utils.Sms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
public class RegisterController {

    @Autowired
    private UserService userService;

    @GetMapping("/register")
    public String register(){
        return "register";
    }

    @PostMapping("/register/sendCode")
    @ResponseBody
    public String sendCode(String operate,String data){
        if ("mailBox".equals(operate)){
            String code = MailBox.getCode(data);
            return code;
        }else {
            String code = Sms.send(data);
            return code;
        }
    }

    @SystemLog(value = "新用户注册",status = "user")
    @PostMapping("/register/email")
    @ResponseBody
    public boolean registerEmail(String email,String password){
        String name = email.split("@")[0];
        User user = new User();
        UserStatus userStatus = new UserStatus();
        userStatus.setId(1);
        userStatus.setName("用户");
        user.setuName(name);
        user.setuAccount(name);
        user.setuEmail(email);
        user.setuPassword(password);
        user.setUserStatus(userStatus);
        user.setuTime(new Date());
        boolean flag = userService.addUser(user);
        return flag;
    }

    @SystemLog(value = "新用户注册",status = "user")
    @PostMapping("/register")
    @ResponseBody
    public boolean register(String name,String password){
        User user = new User();
        UserStatus userStatus = new UserStatus();
        userStatus.setId(1);
        user.setuName(name);
        user.setuAccount(name);
        user.setuPassword(password);
        user.setUserStatus(userStatus);
        user.setuTime(new Date());
        boolean flag = userService.addUser(user);
        return flag;
    }

    @SystemLog(value = "新用户注册",status = "user")
    @PostMapping("/register/Sms")
    @ResponseBody
    public boolean registerSms(String phone,String password){
        User user = new User();
        UserStatus userStatus = new UserStatus();
        userStatus.setId(1);
        user.setuName(phone);
        user.setuAccount(phone);
        user.setuPhone(phone);
        user.setuPassword(password);
        user.setUserStatus(userStatus);
        user.setuTime(new Date());
        boolean flag = userService.addUser(user);
        return flag;
    }

    @PostMapping("/email")
    @ResponseBody
    public Boolean judgeEmail(String email){
        return userService.judgeEmail(email);
    }

    @PostMapping("/account")
    @ResponseBody
    public Boolean judgeAccount(String name){
        return userService.judgeAccount(name);
    }

    @PostMapping("/phone")
    @ResponseBody
    public Boolean judgePhone(String phone){
        return userService.judgePhone(phone);
    }
}
